// 引入 QCloud 小程序增强 SDK
var qcloud = require('../../../../vendor/qcloud-weapp-client-sdk/index');

// 引入配置
var config = require('../../../../config');

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
});

// 显示成功提示
var showSuccess = text => {
    wx.hideToast();
    wx.showToast({
        title: text,
        icon: 'success'
    });
};

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    });
};

var showMsg = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: content,
        showCancel: false
    });
};
//获取应用实例
var app = getApp()
var busy=false;
Page({
  data: {
      hash_key:"",
      loading:false,
      start:false,
      text:"",
      is_mine:false,
      imageSrc:"",
      nicker_name:"",
      animationData: {},
      num:5
  },
  Interval:function(){
       var self=this;
        this.data.intervarID= setInterval(function(){
                self.animation.scale(2, 2).step()
                self.animation.scale(1, 1).step()
                self.setData({
                    animationData: self.animation.export()
                })
                self.setData({
                    num:self.data.num-1,
                });
                if(self.data.num == 0){
                    clearInterval(self.data.intervarID);
                    setTimeout(function(){
                        wx.switchTab({
                            url: '/pages/person/index/index'
                        })
                    }, 1000);
                }
            },1000);
  },
  onLoad: function (e) {
    console.log('onLoad');
    if(busy){
      return;
    }
    this.setData({
        hash_key:e.hash_key,
        num:5
    })
    busy=true;
    var animation = wx.createAnimation({
      duration: 100,
        timingFunction: 'ease',
    })
    this.animation = animation
    var self=this;
    app.getUserInfo(function(userInfo){
      console.log(userInfo);
        qcloud.request({
                // 要请求的地址
                url: config.service.apiUrl+'messages/'+self.data.hash_key,
                success(result) {
                console.log(result);
                if(result.statusCode==200){
                    if(result.data.success==true){
                        if(result.data.message_data.dead==0){
                            if(result.data.is_mine==true){
                                self.setData({
                                        is_mine:true
                                });
                            }else{
                                self.setData({
                                        is_mine:false
                                });
                            }
                            self.setData({
                                    start:true,
                                    text:result.data.message_data.text,
                                    imageSrc:result.data.message_data.image_url,
                                    nicker_name:result.data.message_data.user.nickname
                            });
                            if(result.data.message_data.type==2){
                                self.setData({
                                        loading:true
                                });
                            }
                            if(result.data.message_data.image_url==""&&result.data.is_mine==false){
                                self.Interval();
                            }
                        }else{
                            if(result.data.message_data.is_qun==1){
                                wx.hideToast();
                                wx.showModal({
                                    title:'提示信息',
                                    content: '你来晚了，本密件已经被销毁',
                                    showCancel: false,
                                    success: function(res) {
                                        if (res.confirm) {
                                            wx.switchTab({
                                                url: '/pages/person/index/index'
                                            })
                                        }
                                    }
                                });
                            }else{
                                wx.switchTab({
                                    url: '/pages/person/index/index'
                                })
                            }
                        }
                    }else{
                        wx.hideToast();
                        wx.showModal({
                            title:'提示信息',
                            content: result.data.message,
                            showCancel: false,
                            success: function(res) {
                                if (res.confirm) {
                                    wx.switchTab({
                                        url: '/pages/person/index/index'
                                    })
                                }
                            }
                        });
                    }
                }else{
                    wx.switchTab({
                        url: '/pages/person/index/index'
                    })
                }
                },

                fail(error) {
                    console.log('request fail', error);
                },

                complete() {
                    busy=false;
                    console.log('request complete');
                }
        });
    });
  },
  onShow: function () {
    console.log('onShow');
  },
  imageLoad: function(e){
    console.log(e);
    var self=this;
    self.setData({
            loading:false
    });
    if(self.data.is_mine==false){
        self.Interval();
    }
  }
})
